
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(NavMeshAgent))]
public abstract class CharacterControl<T> : StateBehaviour<T> where T : CharacterControl<T>
{
    #region Attributes

    /* Stats */
    public float walkSpeed = 3.0f;
    public float runSpeed = 7.0f;

    /*  Control */
    private NavMeshAgent controller;

    #endregion

    #region Props

    public NavMeshAgent Controller 
    { 
        get { return this.controller; } 
    }

    public Vector2 Position2D
    {
        get
        {
            return this.ToVector2(this.transform.position);
        }
    }

    public Vector2 Forward2D
    {
        get
        {
            return this.ToVector2(this.transform.forward);
        }
    }

    #endregion

    #region Methods

    public void Init(State<T> state)
    {
        this.rigidbody.useGravity = false;
        this.rigidbody.isKinematic = true;
        this.controller = this.GetComponent<NavMeshAgent>();
        this.State = state;
    }

    public void PlayAnimation(string name)
    {
        this.animation.CrossFade(name);
    }

    public void PlaySound(string name)
    {
    }

    public bool IsOverPoint(Vector3 targetPoint, float offset)
    {
        Vector3 distance = this.ToVector2(targetPoint) - this.Position2D;
        return distance.magnitude < offset;
    }

    public Vector2 ToVector2(Vector3 targetPoint)
    {
        return new Vector2(targetPoint.x, targetPoint.z);
    }

    public void Move(Vector3 direction, float speed)
    {
        this.Controller.Move(direction * speed * Time.deltaTime);
    }

    public void MoveTo(Vector3 destination, float speed)
    {
        this.Controller.speed = speed;
        this.Controller.destination = destination;
    }

    #endregion
}
